package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.ClassDao;
import com.example.domain.Classes;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.ClassService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 班级服务实现层
 * @date 2019-12-31
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    ClassDao classDao;

    /**
     * @describe 添加班级信息
     */
    @Override
    public Result addClass(Classes classes) {
        if (classes != null) {
            //获取系统当前时间作为记录创建时间
            classes.setRecordTime(DateTool.getTimestamp());
        }
        //调用JPA保存实体方法
        Classes classes1 = classDao.save(classes);

        if (classes1 != null) {
            return ResultFactory.buildSuccessResult(classes1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新班级信息
     */
    @Override
    public Result updateClass(Classes classes) {
        if (classes != null) {
            //获取系统当前时间作为记录更新时间
            classes.setUpdateTime(DateTool.getTimestamp());
        }
        //调用JPA保存并刷新实体方法
        Classes classes1 = classDao.saveAndFlush(classes);

        if (classes1 != null) {
            return ResultFactory.buildSuccessResult(classes1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除班级信息
     */
    @Override
    public Result deleteClassById(String classId) {
        int row = classDao.deleteByClassId(Integer.parseInt(classId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有班级信息
     */
    @Override
    public Result queryAllClass() {
        List<Classes> list = classDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 根据编号查询班级信息
     */
    @Override
    public Result queryClassById(String classId) {
        List<Classes> list = classDao.findByClassId(Integer.parseInt(classId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有班级信息和学期信息
     */
    @Override
    public Result queryAllClassAndTerm() {
        List<Map<String, Object>> list = classDao.queryAllClassAndTerm();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加班级信息
     */
    @Override
    public Result addClassByExcel(List<Classes> list) {
        List<Classes> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = classDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据班级编号批量删除班级信息
     */
    @Override
    public Result deleteClassByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = classDao.deleteByClassIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 班级信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
//		System.out.println(info[0]);
        if (info[0].equals("termName")) {

            list = classDao.findByTermNameLike(info[1]);

        } else if (info[0].equals("className")) {

            list = classDao.findByClassNameLike(info[1]);

        } else if (info[0].equals("typeName")) {
            list = classDao.findByTypeNameLike(info[1]);
        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }
}
